package csc.traning.wpsj.servlet;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import csc.traning.wpsj.dao.User;
import csc.traning.wpsj.dao.UserDB;

/**
 * Servlet implementation class RegisterUser
 */
public class RegisterUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if("Register".equals(request.getParameter("register"))){
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String email = request.getParameter("email");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String confirm = request.getParameter("confirm");
			String phone = request.getParameter("phone");
			
			RequestDispatcher dispatch = request.getRequestDispatcher("profile_register.jsp");
			
			String message = "";
			
			if(firstname == null || firstname.isEmpty()){
				message += "Firstname is required! <br />";
			}
			
			if (lastname == null || lastname.isEmpty()) {
				message += "Lastname is required! <br />";
			}
			
			if(email == null || email.isEmpty()){
				message += "Email is required! <br/>";
			}
			else{
				if(!isValidEmailAddress(email)){
					message += "Email is not valid! <br />";
				}
			}
			if(username == null || username.isEmpty()){
				message += "Username is required! <br />";
			}
			else{
				if(UserDB.checkUser(username)){
					message += "Username is existed!<br />";
				}
			}
			if(password == null || password.isEmpty()){
				message += "Password is required! <br />";
			}else{
				if(confirm == null || confirm.isEmpty()){
					message += "Confirm password is required! <br />";
				}
				else{
					if(!password.equals(confirm)){
						message += "Confirm password not matched! <br />";
					}
				}
			}
			
			if(!message.isEmpty()){
				request.setAttribute("message", message);
				dispatch.forward(request, response);
			}else{
				User user = new User(username, password, firstname, lastname, phone, email, "employee");
				UserDB.addUser(user);
				request.setAttribute("success", "Successfully register! Thanks. <a href='../logon.jsp'>Go back to login</a>");
				dispatch.forward(request, response);
			}
		}
		else{
			response.sendRedirect("../logon.jsp");
		}
	}
	
	public boolean isValidEmailAddress(String email) {
		final String EMAIL_PATTERN = 
				"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern pat = Pattern.compile(EMAIL_PATTERN);
		Matcher match = pat.matcher(email);
		return match.matches();
	}
	
}
